import {getNewsList} from "../../utils/api.js";
import {tsFormatTime} from "../../utils/util.js";
Page({
  data: {
    newsList:[],
    isMore: true,
    loadingPage: 1,
    loadingPageSize: 6
  },
  jump: function(e){
    var id = e.currentTarget.dataset.viewid
    wx.navigateTo({
      url: '../../pages/view-text/view-text?id='+id,
    })
  },
  onLoad: function(options){
    getNewsList(this.data.loadingPageSize, this.data.loadingPage, (data) => {
      for(let i=0; i<data.length; i++){
        data[i].edittime = tsFormatTime(data[i].edittime, "Y-M-D h:m");
      }
      console.log(data);
      this.setData({
        newsList: data
      })
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      newsList: [],
      lisMore: true,
      loadingPage: 1,
    })
    getNewsList(this.data.loadingPageSize, this.data.loadingPae, (data) => {
      for (let i = 0; i < data.length; i++) {
        data[i].edittime = tsFormatTime(data[i].edittime, "Y-M-D h:m");
      }
      this.setData({
        newsList: data
      });
      // 隐藏导航栏加载框
      wx.hideNavigationBarLoading();
      // 停止下拉动作
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '玩命加载中...',
    });
    var temp = this.data.loadingPage;
    temp++;
    this.setData({
      loadingPage: temp
    })
    console.log(this.data.loadingPage);
    getNewsList(this.data.loadingPageSize, this.data.loadingPage, (data) => {
      for (let i = 0; i < data.length; i++) {
        data[i].edittime = tsFormatTime(data[i].edittime, "Y-M-D h:m");
      };
      var temp = this.data.newsList;
      for(var i=0; i<data.length;i++){
        temp.push(data[i]);
      }
      this.setData({
        newsList: this.data.newsList
      });
      if(data.length == 0){
        this.setData({
          isMore: false
        })
      }
      wx.hideLoading();
    })
  }
})